import React from 'react';
import { Card, Form, message, Table } from 'antd';
import SearchForm from '../../components/AutoForm/SearchForm';
import { connect } from 'dva';

@connect(({ incomeOrder, loading }) => ({
  tableLoading: loading.effects['incomeOrder/page'],
  tableData: incomeOrder.auditPageList,
  pagination: incomeOrder.pagination,
  columns: incomeOrder.columnsAudit,
  searchFields: incomeOrder.searchFields,
  searchValues: incomeOrder.searchValues,
  editId: incomeOrder.editId,
  selectionIds: incomeOrder.selectionIds,
  selectedRows: incomeOrder.selectedRows,
}))
class EditableTable extends React.Component {
  rowClassName = (record, index) => {
    if (record.status === 'GATHERING_AUDIT') {
      return 'orderParent';
    }
  };

  componentDidMount() {
    this.handleSearch();
  }
  handleSearchChange = fields => {
    const { dispatch } = this.props;
    dispatch({
      type: 'incomeOrder/handleSearchChange',
      payload: fields,
    });
  };

  handleSearch = (flag) => {
    const { dispatch, pagination, searchValues } = this.props;
    let pageParam = { pageSize: pagination.pageSize, current: flag==='btn' ? 1 : pagination.current };
    let searchParam = {};
    Object.keys(searchValues).map(key => {
      searchParam[key] = searchValues[key]['value'];
    });
    const param = {
      ...searchParam,
      ...pageParam,
    };
    dispatch({
      type: 'incomeOrder/auditPage',
      payload: param,
    });
  };

  onChange = (selectedRowKeys, selectedRows) => {
    const { dispatch } = this.props;
    dispatch({
      type: 'incomeOrder/handleSelection',
      payload: { selectedRowKeys, selectedRows },
    });
  };

  onSend = () => {
    const { dispatch, selectionIds } = this.props;
    if (!selectionIds.length) {
      return message.error('请选择一行要发送的数据');
    }
    dispatch({
      type: 'incomeOrder/audit',
      payload: selectionIds,
    }).then(() => {
      this.handleSearch();
    });
  };

  onReject = () => {
    const { dispatch, selectionIds } = this.props;
    if (!selectionIds.length) {
      return message.error('请选择一行要驳回的数据');
    }
    dispatch({
      type: 'incomeOrder/reject',
      payload: selectionIds,
    }).then(() => {
      this.handleSearch();
    });
  };

  render() {
    const {
      columns,
      pagination,
      tableLoading,
      tableData,
      searchFields,
      searchValues,
      dispatch,
    } = this.props;
    columns.forEach((c,i) => {
      if(c.title === '操作'){
          columns.splice(i,1);
      }
    })
    pagination.onChange = (page, pageSize) => {
      let searchParam = {};
      Object.keys(searchValues).map(key => {
        searchParam[key] = searchValues[key]['value'];
      });
      dispatch({
        type: 'incomeOrder/auditPage',
        payload: {
          ...searchParam,
          current: page,
          pageSize,
        },
      });
    };
    pagination.onShowSizeChange = (current, size) => {
      let searchParam = {};
      Object.keys(searchValues).map(key => {
        searchParam[key] = searchValues[key]['value'];
      });
      dispatch({
        type: 'incomeOrder/auditPage',
        payload: {
          ...searchParam,
          current: current,
          pageSize: size,
        },
      });
    };

    const rowSelection = {
      type: 'checkbox',
      selections: this.props.selectedRows,
      selectedRowKeys: this.props.selectionIds,
      onChange: this.onChange,
      getCheckboxProps: record => ({
        disabled: record.status !== 'GATHERING_AUDIT', // Column configuration not to be checked
      }),
    };

    return (
      <Card bodyStyle={{ padding: 0 }}>
        <SearchForm
          header="审核收入->操作"
          layout="inline"
          isSend={true}
          isReject={true}
          onFieldsChange={this.handleSearchChange}
          onSubmit={() => {this.handleSearch('btn')}}
          onSend={this.onSend}
          onReject={this.onReject}
          fields={searchFields}
          fieldValue={searchValues}
        />

        <Table
          bordered
          rowKey="id"
          loading={tableLoading}
          dataSource={tableData}
          columns={columns}
          rowSelection={rowSelection}
          rowClassName={this.rowClassName}
          scroll={{ x: 'max-content' }}
          size="small"
          pagination={pagination}
        />
      </Card>
    );
  }
}

const EditableFormTable = Form.create()(EditableTable);
export default EditableFormTable;
